package edu.nus.iss.t5.jass.view;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import edu.nus.iss.t5.jass.controller.JobAdvertisementController;
import edu.nus.iss.t5.jass.controller.*;

/**
 * This is Search Panel class for searching Job Advertisement.  
 * It invoke controller class to retrieve Job Advertisement.
 * It sends data to result panel for display.
 * It implements ActionListener interface. 
 */
public class SearchPanel extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JPanel searchJP ;
	JPanel ButtonsPanel ; 
	JPanel searchButtonJP;
	JPanel ResetButtonJP;
	JPanel addAdvertisementJP;
	JPanel titlePanel;
	JLabel titleLB;
	JLabel descLB;
	JButton searchButton;
	JButton ResetButton;
	JButton AddAdvertisementButton;
	final JTextField searchTitleTF, searchdescTF;
	final static boolean RIGHT_TO_LEFT = false;
	private JobAdvertisementController cont;
	private ResultPanel res;
	
	SearchPanel() {
		titleLB = new JLabel("Job Title	  :");
		titleLB.setSize(20,10);
		descLB = new JLabel("Description : ");
		descLB.setSize(20,10);
		searchTitleTF = new JTextField(50);
		Dimension d = new Dimension(50, 20);
		searchTitleTF.setSize(d);
		searchdescTF = new JTextField(50);
		searchdescTF.setSize(d);
		searchButton = new JButton("Search");
		searchButton.setSize(d);
		searchButton.addActionListener(this);
		ResetButton = new JButton("Reset");
		ResetButton.addActionListener(this);
		AddAdvertisementButton = new JButton("Add Advertisement");
		AddAdvertisementButton.addActionListener(this);
		addComponentsToPane(new JPanel());
		this.add(searchJP);
        
	}
   // public void setController (JobAdvertisementController c){
   // 	this.cont = c;
   // }
	/**
	 * Register Result Panel  
	 * This is to register result panel for result display.
	 * 
	 */	
    public void registerResultPanel(ResultPanel r){
    	this.res = r;
    }
	public void addComponentsToPane(Container pane) {
		
		 if (RIGHT_TO_LEFT) {
	            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
	        }

		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		// first row below
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		pane.add(titleLB, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
	    c.gridy = 0;
		
		pane.add(searchTitleTF, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 0;
		pane.add(new JLabel(""), c);
		
		// second row below
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		pane.add(descLB, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 1;
		pane.add(searchdescTF, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 2;
		c.gridy = 1;
		pane.add(new JLabel(""), c);
		
        // Third row below
		ButtonsPanel = new JPanel(new GridLayout(1,1));
		searchButtonJP = new JPanel();
		searchButton.setPreferredSize(new Dimension(90, 28));
		searchButtonJP.add(searchButton);
		
		ButtonsPanel.add(searchButtonJP);
		
		ResetButtonJP = new JPanel();
		ResetButton.setPreferredSize(new Dimension(90, 28));
		ResetButtonJP.add(ResetButton);
		ButtonsPanel.add(ResetButtonJP);
		
		addAdvertisementJP = new JPanel();
		AddAdvertisementButton.setPreferredSize(new Dimension(150, 28));
		addAdvertisementJP.add(AddAdvertisementButton);
		ButtonsPanel.add(addAdvertisementJP);
		
		
		searchJP = new JPanel(new BorderLayout());
		//searchJP.setPreferredSize(new Dimension(1200,130));
		Toolkit tk = Toolkit.getDefaultToolkit();  
		int xSize = ((int) tk.getScreenSize().getWidth()); 
		searchJP.setPreferredSize(new Dimension(xSize - 8, 120));
		
		titlePanel = new JPanel();
		titlePanel.setVisible(true);
		titlePanel.setPreferredSize(new Dimension(xSize - 8, 25));
		titlePanel.setBorder(
				new TitledBorder(new LineBorder(Color.GRAY, 1, false),"Search Job Advertisement Here", TitledBorder.LEFT, TitledBorder.BELOW_TOP));		
		searchJP.add(titlePanel, BorderLayout.NORTH);
		searchJP.add(pane, BorderLayout.CENTER);
		searchJP.add(ButtonsPanel, BorderLayout.SOUTH);
		searchJP.setBorder(new LineBorder(Color.GRAY));
				
	}
	
	/**
	 * Implementation of ActionListener interface. 
	 * Captures all button actions. 
	 */
	public void actionPerformed(ActionEvent event) {
             if (event.getSource() == AddAdvertisementButton) {
            
            MainFrame mainFrame = new MainFrame();
            AddPanel addPanel = new AddPanel();
            mainFrame.setaddPanel(addPanel);
            Toolkit tk = Toolkit.getDefaultToolkit();
            int xSize = ((int) tk.getScreenSize().getWidth());
            int ySize = ((int) tk.getScreenSize().getHeight());
            addPanel.setSize(xSize-410, ySize-310);
            mainFrame.setSize(xSize-400, ySize-300);
            mainFrame.setTitle("Add JobAdvertisement");
            mainFrame.setVisible(true);
        }
        if (event.getSource().equals(searchButton) ) {
                 FormSearchAdvertisement fj = new FormSearchAdvertisement(searchTitleTF.getText(),searchdescTF.getText() );
                 //System.out.println(fj.title);
                 //System.out.println(fj.description);
                 cont = JobAdvertisementController.getInstance ();
                 try {
                	 res.displayAdList(cont.StartSearchAdvertisement(fj));
                 }
                 catch (NullPointerException e){
                	 //null pointer exception
                 }
         }
        if (event.getSource().equals(ResetButton) ) {
        	searchTitleTF.setText("");
        	searchdescTF.setText("");
        }        
        
	}

}
